#ifndef _SERVER_HPP_
#define _SERVER_HPP_

#include "queue.hpp"
#include "time_providers.hpp"
#include <iostream>
using namespace std;

class Server : public Queue {
public:
  Server(TP *time_provider, rng01_t &r, string name)
    : Queue(time_provider, r, name) {}

  virtual void departure(Job *leaving, double t) {
    update_accumulators(t);
#ifdef LOG
    //TODO: assert leaving = jobs.front(); //the first job leaves the queue
#endif
    jobs.pop();
    --n_jobs;
    //schedule next departure
    last_event_time = t;
    if(n_jobs > 0)
      schedule_departure(jobs.front());
    //update indexes
    throughput = (double)++n_departures / t;
    waiting_time = area / n_departures;
  }

  virtual void arrival(Job *job, double t) {
    update_accumulators(t);
    last_event_time = t;
    //push the job into the queue
    push(job);
    ++n_jobs;
    //schedule the departure if single-job case
    if(n_jobs == 1)
      schedule_departure(job);
  }

private:
  queue<Job *> jobs;

  void push(Job *job) {
    jobs.push(job);
  }
};
#endif
